package com.example.apollotogit.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 描述：redis配置类
 *
 * @author 庄星宇
 * @date 2021.11.05
 */
@Configuration
public class RedisConfig {

    /**
     * redis 服务器地址
     */
    @Value("${spring.redis.host}")
    private String host;

    /**
     * redis 端口号
     */
    @Value("${spring.redis.port}")
    private int port;

    /**
     * redis 服务器密码
     */
    @Value("${spring.redis.password}")
    private String password;

    /**
     * redis 连接池最大连接数(使用负值无限制)
     */
    @Value("${spring.redis.pool.max-active}")
    private int maxActive;

    /**
     * redis 连接池最大空闲数
     */
    @Value("${spring.redis.pool.max-idle}")
    private int maxIdle;

    /**
     * redis 连接池小空闲数
     */
    @Value("${spring.redis.pool.min-idle}")
    private int minIdle;

    /**
     * redis 连接池最大阻塞等待时间(负值无限制)
     */
    @Value("${spring.redis.pool.max-wait}")
    private int maxWait;

    /**
     * redis 数据库索引(默认0)
     */
    @Value("${spring.redis.database}")
    private int database;

    /**
     * redis 超时时间
     */
    @Value("${spring.redis.timeout}")
    private int timeout;

    @Value("${redis.key}")
    private String myKey;


    @Bean
    public JedisPoolConfig getRedisConfig() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxActive);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setMaxWaitMillis(maxWait);
        return config;
    }

    @Bean
    public JedisConnectionFactory getConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, port);
        redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
        return new JedisConnectionFactory(redisStandaloneConfiguration);
    }

    @Bean
    public RedisTemplate<?, ?> getRedisTemplate() {
        JedisConnectionFactory factory = getConnectionFactory();
        RedisTemplate<?, ?> redisTemplate = new StringRedisTemplate(factory);
        return redisTemplate;
    }

//    @Bean
//    public RedisMessageListenerContainer redisMessageListenerContainer() throws Exception {
//        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
//        container.setConnectionFactory(getConnectionFactory());
//        return container;
//    }

    // 创建基本的key监听器
//    @Bean
//    public RedisKeyChangeListener redisKeyChangeListener() throws Exception {
//        RedisKeyChangeListener listener = new RedisKeyChangeListener(redisMessageListenerContainer(), myKey);
//        return listener;
//    }
}